/**
 * @param {string} url
 * @returns {Object}
 */
export function param2Obj(url) {
  const search = url.split("?")[1];
  if (!search) {
    return {};
  }
  return JSON.parse(
    '{"' +
      decodeURIComponent(search)
        .replace(/"/g, '\\"')
        .replace(/&/g, '","')
        .replace(/=/g, '":"')
        .replace(/\+/g, " ") +
      '"}'
  );
}

// 导出时间格式化
// "YYYY-mm-dd-时间戳"
export function dateFormat(date) {
  let ret,
    fmt = "YYYY-mm-dd";
  const opt = {
    "Y+": date.getFullYear().toString(), // 年
    "m+": (date.getMonth() + 1).toString(), // 月
    "d+": date.getDate().toString(), // 日
  };
  for (let k in opt) {
    ret = new RegExp("(" + k + ")").exec(fmt);
    if (ret) {
      fmt = fmt.replace(
        ret[1],
        ret[1].length == 1 ? opt[k] : opt[k].padStart(ret[1].length, "0")
      );
    }
  }
  return fmt + "-" + countDown(date);
}

// 时分秒转为时间戳
function countDown(date) {
  let s = 0;
  let hour = date.getHours();
  let min = date.getMinutes();
  let sec = date.getSeconds();
  s = Number(hour * 3600) + Number(min * 60) + Number(sec);
  return s;
}
